﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoftKeyboardSample
{
    public partial class SoftKeyboard : Form
    {
        private bool leftFlag = false;
        private Point mouseOffset = new Point();
        private Point _location = new Point(0, 0);
        private IntPtr _parentHandle = IntPtr.Zero;

        public SoftKeyboard()
        {
            InitializeComponent();
        }

        public SoftKeyboard(Point p, IntPtr handle)
        {
            _parentHandle = handle;
            _location = p;
            InitializeComponent();
        }

        public const int USER_MESSAGE_ID = 0x0400;//用户自定义消息的开始数值

        [DllImport("user32.dll")]
        public static extern void PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private const int WS_EX_TOOLWINDOW = 0x00000080;
        private const int WS_EX_NOACTIVATE = 0x08000000;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
                //cp.Parent = IntPtr.Zero; // Keep this line only if you used UserControl
                return cp;
                //return base.CreateParams;
            }
        } 

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panelTop_MouseMove(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(mouseOffset.X, mouseOffset.Y);  //设置移动后的位置
                this.Location = mouseSet;
            }
        }

        private void panelTop_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOffset = new Point(-e.X, -e.Y); //得到变量的值
                leftFlag = true;                  //点击左键按下时标注为true;
            }
        }

        private void panelTop_MouseUp(object sender, MouseEventArgs e)
        {
            if (leftFlag)
            {
                leftFlag = false;//释放鼠标后标注为false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string key = ((Button)sender).Text.Trim();
            SendKeys.Send(key);
        }

        private void buttonBackspace_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{BACKSPACE}");
        }

        private void buttonLeft_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{LEFT}");
        }

        private void buttonRight_Click(object sender, EventArgs e)
        {
            SendKeys.Send("{RIGHT}");
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if(_parentHandle != IntPtr.Zero)
            {
                this.Hide();
                int res = SendMessage(_parentHandle, USER_MESSAGE_ID, 0, 0);
                if(res == 0x00)
                {
                    this.Close();
                }
                else if(res == 0xff)
                {
                    this.Show();
                }                
            }
        }

        private void SoftKeyboard_Load(object sender, EventArgs e)
        {
            if(_location.X > 0 && _location.Y > 0)
            {
                this.Location = _location;
            }
        }
    }
}
